/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wica.view;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnKeyListener;
import android.widget.Button;

import com.wica.facetube.R;


public class TextLocationDialog extends Activity implements OnKeyListener{
	// *************** Constants ************** //
	// Log constant
    protected static final String LOG_TAG = "_FT.MoodDialog_";

	// *************** Attributes ************************* //
    private NotificationManager notificationManager;
	private Button button;

	// *************** Methods **************************** //
	/**
     * Initialization of the Activity after it is first created.  Must at least
     * call {@link android.app.Activity#setContentView setContentView()} to
     * describe what is to be displayed in the screen.
     */
    @Override
	protected void onCreate(Bundle savedInstanceState) {   	
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_LEFT_ICON);

        // Inflate our UI from its XML layout description.
        setContentView(R.layout.text_location_dialog);

        // Get display items for later interaction
        button = (Button) findViewById(R.id.home);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setLocation(R.drawable.menu_location_by_text, R.string.status_bar_notifications_home_message,
                        true);
            }
        });
        button = (Button) findViewById(R.id.work);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setLocation(R.drawable.work, R.string.status_bar_notifications_work_message, true);
            }
        });
        button = (Button) findViewById(R.id.somewhere);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setLocation(R.drawable.somewhere, R.string.status_bar_notifications_somewhere_message, true);
            }
        });        
        
        // Get the notification manager service.
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }// onCreate method
    
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
  	  	}else {
  	  		return super.onKeyDown(keyCode, event);
  	  	}
	}// onKey method
	
    private void setLocation(int locationId, int textId, boolean showTicker) {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(textId);
        String locationString="";
        String tickerText;
        Notification notification;
        PendingIntent contentIntent;
        Editor editor;
        
        // choose the ticker text
        tickerText = showTicker ? getString(textId) : null;

        // Set the icon, scrolling text and timestamp
        notification = new Notification(locationId, tickerText, System.currentTimeMillis());        

        // The PendingIntent to launch our activity if the user selects this notification
        contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, TextLocationDialog.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),
                       text, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        notificationManager.notify(R.layout.status_selection, notification);
        
        //We set the preference mood
        editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        switch (locationId){
        	case R.drawable.menu_location_by_text:
        		locationString="At home";
        		break;
        	case R.drawable.work:
        		locationString="At work";
        		break;
        	case R.drawable.somewhere:
        		locationString="Somewhere else";
        		break;
        }
        
		editor.putString("text_location", locationString);
		editor.commit();
		if (getCallingActivity()!=null){
			Log.v(LOG_TAG, LOG_TAG+" algo hace");
			Intent intent = new Intent(TextLocationDialog.this, (getCallingActivity().getClass()));
			//	        intent.putExtra("searchString", "");
	    	setResult(RESULT_OK,intent);
		}
        finish();
    }// setMood method
    
}// TextLocationDialog class